package com.workpaper.app.plan.business;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.workpaper.app.db.mybatis.MyDB;
import com.workpaper.app.plan.entity.DailyPlan;
import com.workpaper.app.plan.mapper.DailyPlanMapper;
/**
 * 日常计划Dao，使用单例模式
 * @author zyj
 *
 */
public final class DailyPlanBusiness {
	
	private static class Holder{
		private static DailyPlanBusiness INSTANCE = new DailyPlanBusiness();
	}

	private DailyPlanBusiness() {
		super();
	}
	
	public static DailyPlanBusiness getInstance() {
		return Holder.INSTANCE;
	}
	/**
	 * 新增日常计划
	 * @param dailyPlan
	 */
	public void addNewDailyPlan(DailyPlan dailyPlan) {
		if(dailyPlan != null) {
			try(SqlSession sqlSession = MyDB.openSession()){
				DailyPlanMapper mapper = sqlSession.getMapper(DailyPlanMapper.class);
				mapper.saveDailyPlan(dailyPlan);
				sqlSession.commit();
			}
		}
	}
	/**
	 * 查询所有日常计划
	 * @return
	 */
	public List<DailyPlan> getAllDailyPlans(){
		try (SqlSession sqlSession = MyDB.openSession()){
			DailyPlanMapper mapper = sqlSession.getMapper(DailyPlanMapper.class);
			return mapper.getAllDailyPlans();
		}
		//后续需要增加错误处
	}
	
	/**
	 * 根据id查询日常计划
	 * @return
	 */
	public DailyPlan selectById(String id){
		try (SqlSession sqlSession = MyDB.openSession()){
			DailyPlanMapper mapper = sqlSession.getMapper(DailyPlanMapper.class);
			return mapper.selectById(id);
		}
	}
	
	/**
	 * 删除日常计划
	 * @param id
	 * @return
	 */
	public int deleteById(String id) {
		try (SqlSession sqlSession = MyDB.openSession()){
			DailyPlanMapper mapper = sqlSession.getMapper(DailyPlanMapper.class);
			int num = mapper.deleteById(id);
			sqlSession.commit();
			return num;
		}
	}
	
	/**
	 * 根据id更新一条日常计划
	 * @param dailyPlan
	 */
	public int updateById(DailyPlan dailyPlan) {
		try (SqlSession sqlSession = MyDB.openSession()){
			DailyPlanMapper mapper = sqlSession.getMapper(DailyPlanMapper.class);
			int num = mapper.updateById(dailyPlan);
			sqlSession.commit();
			return num;
		}
	}
}
